<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UH_Controller extends CI_Controller{

    public $uid = 0;
    public $mobile;
    public $username;
    public $user_info = false;
    
	public function __construct()
	{
		parent::__construct();
		$this->_init(); //用着重载
		
		$user_cookie = json_decode(base64_decode($this->input->cookie('user_cookie')));
		if ($user_cookie) {
		    $this->load->model('Muser');
		    $user = $this->Muser->userByid(array('user.id'=>$user_cookie->uid))->row();
		    $this->uid        = $user->id;
		    $this->mobile     = $user->mobile;
		    $this->username   = $user->username ? $user->username : $user->mobile;
		    $this->user_info  = $user;
		}
		
		// 开发模式下开启性能分析
		if (ENVIRONMENT === 'development') {
// 			$this->output->enable_profiler(TRUE);
		}
	}
	
	public function _init() {}
	
	/**
	 * @获取测试账号的uid,companyid
	 * */
	public function get_test_account()
	{
	    $this->load->helper('testAccount');
	    $t_uid_arr = array();  //测试用户uid 
	    $t_com_arr = array();  //测试用户companyid
	    $res = $this->Base_model->getWherein('user', 'mobile', UH_mobile());
	    foreach ($res->result() as $r) {
	        $t_uid_arr[] = $r->id;
	        $t_com_arr[] = $r->companyid;
	    }
	    $t_uid_arr = array_values(array_flip(array_flip(array_filter($t_uid_arr))));
	    $t_com_arr = array_values(array_flip(array_flip(array_filter($t_com_arr))));
	    return array('t_uid_arr'=>$t_uid_arr, 't_com_arr'=>$t_com_arr);
	}
	
	/**
	 * @param string/int $errcode：错误码
	 * @param boolean $status：状态
	 * @param mix $data：数据
	 * */
	public function getJson($errcode=10001, $status=false, $data='')
	{
	    $errmsg = is_int($errcode) ? $this->config->item('errmsg_'.$errcode) : $errcode;
	    echo json_encode(array('status'=>$status, 'errmsg'=>$errmsg, 'data'=>$data));
	    exit;
	}
	
	/**
	 * @param number $len:验证码长度
	 * @param string $width:验证码宽
	 * @param string $height:验证码高
	 * @param string $size:验证码字体大小
	 * @return 图片
	 */
	public function create_captcha($len=4, $str='0123456789', $width='74', $height='30')
	{
	    $this->load->helper('captcha');
	    $word = randomStr($len, $str);
	    $config = array(
	        'word'         => $word,
	        'img_path'     => $this->config->upload_image_path('captcha', true),
	        'img_url'      => $this->config->get_web_url('img').'captcha/',
	        'font_path'    => 'assets/css/YHBold.ttf',
	        'img_width'    => $width,
	        'img_height'   => $height,
	        'expiration'   => '1200',
	    );
	    return create_captcha($config);
	}
	
}